package com.example.teaching_server.entity;

import jakarta.persistence.*;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.Max;
import lombok.*;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;
import java.time.LocalTime;
import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * 课程表实体类
 *
 * @author Teaching Server
 * @since 1.0.0
 */
@Getter
@Setter
@ToString(exclude = {"term", "timeSlot", "classEntity", "course", "teacher", "classroom"})
@EqualsAndHashCode(of = "scheduleId")
@Entity
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "schedules", indexes = {
        @Index(name = "idx_schedule_term", columnList = "term_id"),
        @Index(name = "idx_schedule_class", columnList = "class_id"),
        @Index(name = "idx_schedule_teacher", columnList = "teacher_id"),
        @Index(name = "idx_schedule_date", columnList = "specific_date")
})
public class ScheduleEntity {

    /**
     * 课程表ID
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long scheduleId;

    /**
     * 学期信息
     */
    @NotNull(message = "学期不能为空")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "term_id", nullable = false)
    private TermEntity term;

    /**
     * 时间槽信息
     */
    @NotNull(message = "时间槽不能为空")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "slot_id", nullable = false)
    private TimeSlotEntity timeSlot;

    /**
     * 星期几 (1-7 表示周一到周日)
     */
    @NotNull(message = "星期不能为空")
    @Min(value = 1, message = "星期必须在1-7之间")
    @Max(value = 7, message = "星期必须在1-7之间")
    @Column(nullable = false)
    private Integer weekDay;

    /**
     * 班级信息
     */
    @NotNull(message = "班级不能为空")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "class_id", nullable = false)
    private ClassEntity classEntity;

    /**
     * 课程信息
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "course_id")
    private CourseEntity course;

    /**
     * 教师信息
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "teacher_id")
    private TeacherEntity teacher;

    /**
     * 教室信息
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "classroom_id")
    private ClassroomEntity classroom;

    /**
     * 活动类型
     */
    @NotNull(message = "活动类型不能为空")
    @Enumerated(EnumType.STRING)
    @Column(nullable = false)
    private ActivityType activityType;

    /**
     * 周次
     */
    @Min(value = 1, message = "周次必须大于0")
    @Column(name = "week_number")
    private Integer weekNumber;

    /**
     * 具体日期
     */
    @Column(name = "specific_date")
    private LocalDate specificDate;

    /**
     * 显示名称
     */
    @Column(name = "display_name", length = 200)
    private String displayName;

    /**
     * 创建时间
     */
    @CreationTimestamp
    @Column(name = "created_at", updatable = false)
    private LocalDateTime createdAt;

    /**
     * 活动类型枚举
     */
    public enum ActivityType {
        COURSE("课程"),
        MEETING("会议"),
        EXAM("考试"),
        ACTIVITY("活动");

        private final String description;

        ActivityType(String description) {
            this.description = description;
        }

        public String getDescription() {
            return description;
        }
    }

    /**
     * 获取活动类型描述
     */
    public String getActivityTypeDescription() {
        return activityType != null ? activityType.getDescription() : "";
    }

    /**
     * 验证日程是否有效
     */
    public boolean isValid() {
        return weekDay != null && weekDay >= 1 && weekDay <= 7;
    }
}
